package com.mucd.chat.service;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mucd.chat.mapper.FriendMapper;
import com.mucd.chat.service.support.FriendServiceSupport;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.FriendDto;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.vo.Friend;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IGroupsService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @author mucd
 */
@Service
@Slf4j
public class FriendService {

    @Resource
    private FriendMapper friendMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private IGroupsService groupsService;

    @Resource
    private FriendSettingService friendSettingService;
    @Resource
    private FriendServiceSupport friendServiceSupport;

    @SneakyThrows
    public List<Friend> find(String searchText) {
        if (StrUtil.isBlankIfStr(searchText)) {
            throw new ServiceException("搜索条件不能为空");
        }

        //同时查询好友和文章
//        Future<List<FindVo>> findArticle = ThreadUtil.execAsync(() -> findArticle(searchText));
        Future<List<Friend>> findFriend = ThreadUtil.execAsync(() -> findFriend(searchText));

//        List<FindVo> findVos = findArticle.get();

//        findVos.addAll(findFriend.get());

        List<Friend> friends = findFriend.get();

        return friends;
    }

    /**
     * 搜索朋友
     *
     * @param searchText text
     * @return val
     */
    public List<Friend> findFriend(String searchText) {
        List<Friend> find = friendMapper.findFriend(searchText);
        setFindType(find, Constants.FIND_TYPE_FRIEND);

        return find;
    }


    /**
     * 搜索文章
     *
     * @param searchText text
     * @return val
     */
    public List<Friend> findArticle(String searchText) {
        List<Friend> find = friendMapper.findArticle(searchText);
        setFindType(find, Constants.FIND_TYPE_ARTICLE);

        return find;
    }

    public void setFindType(List<Friend> friends, String findType) {
        friends.forEach(item -> {
            item.setFindType(findType);
        });
    }


    /**
     * 添加好友
     *
     * @param friend 添加好友的信息
     * @return obj
     */
    public Object addFriend(Friend friend) {
        friend.setSendUserId(SecurityUtils.getUserId());
        // todo 根据user1 和 user2的id分别查出手机号,存起来
        SysUser sysUser1 = sysUserMapper.selectUserById(friend.getSendUserId());
        //对方好友信息
        SysUser sysUser2 = sysUserMapper.selectUserById(friend.getReceiveUserId());

        if (ObjectUtil.isNull(sysUser2)) {
            throw new ServiceException("对方账户不存在,请刷新");
        }
        friendSettingService.checkSetting(friend.getReceiveUserId(), Constants.FRIEND_SETTING_TYPE_ALLOW_ADD_FRIEND,"对方不允许添加好友");


        friend.setPhonenumber1(sysUser1.getPhonenumber());
        friend.setPhonenumber2(sysUser2.getPhonenumber());
        friend.setStatus(Constants.RELATION_STATUS_PENDING);

        int row = friendMapper.addFriend(friend);
        log.info("addFriend rows:{}", row);

        return row;
    }

    /**
     * 查询两人是否已经是好友
     */
    public Object checkFriend(Long userId) {
        return null;
    }

    /**
     * 查询好友请求和群通知请求
     *
     * @return obj
     */
    public Object selectNoticeCount() {
        long userId = SecurityUtils.getUserId();
        //有可能同一个好友添加多次,这样只算一个

        List<Friend> friendRelations = friendMapper.selectFriendNoticeCount(userId);
//        List<Friend> friendCount = friendServiceSupport.checkRelationStatus(friendRelations);

        //群组通知
        int groupsCount = 0;

        Map<String, Integer> resMap = new HashMap<>();
        resMap.put("friendCount", friendRelations.size());
        resMap.put("groupsCount", groupsCount);
        return resMap;
    }


    public Object selectFriendNew() {
        long userId = SecurityUtils.getUserId();
        List<Friend> friendRelations = friendMapper.selectFriendNew(userId);

        //同一个好友的多次请求,只保留最新的一条请求
//        friendRelations = friendServiceSupport.checkRelationStatus(friendRelations);

        friendServiceSupport.setRelationStation(friendRelations);

        return friendRelations;
    }


    public Object confirmOrRejectFriend(FriendDto dto) {

        Friend friend = new Friend();
        friend.setRelationStatus(dto.getRelationStatus());
//        friend.setRelationStatus(Constants.RELATION_STATUS_CONFIRM);
        friend.setRelationId(dto.getRelationId());

        int row = friendMapper.confirmFriend(friend);

        return row;
    }


    public Object selectFriendList() {
        long userId = SecurityUtils.getUserId();
        List<Friend> list = friendMapper.selectFriendList(userId);

        return list;
    }
}
